import click
from faker import Faker
from simple_blog import db, app
from simple_blog.models import Post


@app.cli.command()
@click.option('--drop', default=False)
def initdb(drop):
    if drop:
        click.confirm(text='this will delete the database, continue?', abort=True)
        db.drop_all()
        click.echo('Drop tables')
    db.create_all()
    click.echo('Init database')


@app.cli.command()
@click.option('--count', default=5)
def addfakedata(count):
    fake = Faker()
    for i in range(count):
        post = Post(
            title=fake.name(),
            body = fake.sentence(),
            timestamp=fake.date_time_this_year()
        )
        print(post)
        db.session.add(post)
    db.session.commit()
            
